import 'dart:async';

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class ListViewDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      theme: new ThemeData(
        primaryColor: Colors.blue,
      ),
      home: new ListViewWidget(),
    );
  }
}

class ListViewWidget extends StatefulWidget {
  @override
  // ignore: wrong_number_of_type_arguments
  _ListViewState createState() {
    // TODO: implement createState
    return new _ListViewState();
  }
}

class _ListViewState extends State<ListViewWidget> {
  List<JuanKuanBean> widgets = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData("0");
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
        backgroundColor: Colors.brown,
        body: RefreshIndicator(
            child: new ListView.builder(
                itemCount: widgets.length,
                itemBuilder: (BuildContext context, int position) {
                  return new Padding(
                      padding: EdgeInsets.all(10.0),
                      child: new Column(
                        children: <Widget>[
                          new Row(
                            children: <Widget>[
                              new Text(widgets[position].name),
                              new Expanded(
                                  child: new Padding(
                                padding: EdgeInsets.only(left: 10.0),
                                child: new Text(position.toString()),
                              )),
                              new Text(widgets[position].money)
                            ],
                          ),
                          new Padding(
                            padding: EdgeInsets.only(top: 5.0),
                            child: new Divider(height: 1.0, color: Colors.blue),
                          )
                        ],
                      ));
                }),
            onRefresh: () {
              widgets.clear();
              return loadData("5");
            }));
  }

  static const String HOST_BASE =
      "http://47.98.100.243/xunbao/apicishan/allCiShanShouYi";

//  static const String HOST_BASE = "https://jsonplaceholder.typicode.com/posts";

  Future<Null> loadData(String start) async {
    http.Response response =
        await http.post(HOST_BASE, body: {"start": start, "limit": "5"});
    print(response.body);
    var jsonRes = json.decode(response.body);
    String code = jsonRes["code"];
    String msg = jsonRes["msg"];
    var data = jsonRes["data"];

    for (var item in data) {
      widgets.add(JuanKuanBean(item["juanzengren"], item["money"]));
    }
    setState(() {});
  }
}

class JuanKuanBean {
  String name;
  String money;

  JuanKuanBean(this.name, this.money);
}
